home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / listsubs.arc / LISTSUBS.PAS < prev   
Pascal/Delphi Source File  |  1986-01-19  |  6KB  |  199 lines

  1. program listSubs;
  2.  
  3. {
  4. This program prints a listing of all procedure and function
  5. declarations in a Pascal source  program.
  6.  
  7. Author: Fritz Ziegler
  8. Date: 7/15/84, modified 1/19/86
  9. Application: All systems
  10. Originally published in TUG Lines (Turbo User Group) Vol. 1, Issue 5
  11. }
  12.  
  13. type
  14.   fil_type = text;
  15.   filname_type = string[14]; { x:yyyyyyyy.zzz }
  16.   fil_lin_type = string[255];
  17.   maxstring = string[255];
  18.   identifier_type = string[127];
  19.  
  20. var
  21.   fil : fil_type;
  22.   outfil : fil_type;
  23.   filname : filname_type;
  24.   outfilname : filname_type;
  25.   done,
  26.   error : boolean;
  27.  
  28. procedure close_files(var fil,outfil:fil_type);
  29. begin
  30.   close(fil);
  31.   close(outfil);
  32. end; { close_files }
  33.  
  34. procedure upc_filname(var filname:filname_type);
  35.   var
  36.     i : integer;
  37. begin
  38.   for i := 1 to length(filname) do filname[i] := upcase(filname[i]);
  39. end; { upc_filname }
  40.  
  41. procedure get_filnames(var filname,outfilname:filname_type;
  42.                        var done,error:boolean);
  43. var temp : filname_type;
  44. begin
  45.   filname:=''; outfilname:='';
  46.   writeln;
  47.   write('List procedures and functions on what file (CR to quit) ? ');
  48.   readln(filname);
  49.   writeln;
  50.   if filname <> '' then
  51.     begin
  52.       upc_filname(filname);
  53.       if (pos('.',filname)-1 > 0) then
  54.         outfilname:=copy(filname,1,pos('.',filname)-1) + '.LST'
  55.       else outfilname:=filname + '.LST';
  56.  
  57.       write('List to (CR to choose ',outfilname,', LPT1 for printer) ? ');
  58.       readln(temp);
  59.       if (temp <> '') then
  60.         begin
  61.           upc_filname(temp);
  62.           outfilname:=temp;
  63.         end;
  64.       if outfilname=filname then
  65.         begin
  66.           writeln('ERROR >> The source, ',filname,' = the destination, ',outfilname);
  67.           error:=true
  68.         end;
  69.     end { if filname <> '' then }
  70.   else done:=true
  71. end; { get_filename }
  72.  
  73. procedure open_file(var filname:filname_type;var fil:fil_type;var error:boolean);
  74. begin
  75.   {$I-}
  76.   assign(fil,filname);
  77.   reset(fil);
  78.   {$I+}
  79.   if ioResult <> 0 then
  80.     begin
  81.       error:=true;
  82.       writeln('ERROR >> File does not exist');
  83.    end;
  84. end; { open_files }
  85.  
  86. procedure open_outfile(var outfilname:filname_type;var outfil:fil_type);
  87. begin
  88.     assign(outfil,outfilname);
  89.     rewrite(outfil);
  90. end;
  91.  
  92. procedure print_procfunc_list(var fil:fil_type;
  93.                                   filname:filname_type);
  94. var
  95.   fil_lin : fil_lin_type;
  96.   first_word : identifier_type;
  97.   is_cont_lin : boolean;
  98.  
  99.   function is_procfunc(var fil_lin:fil_lin_type;
  100.                        var is_cont_lin:boolean):boolean;
  101.  
  102.  
  103.     procedure get_first_word(fil_lin:fil_lin_type;
  104.                              var first_word:identifier_type);
  105.     label return;
  106.     var
  107.       i, i2 : integer;
  108.     begin { get_first_word }
  109.       first_word:='';
  110.       for i:=1 to length(fil_lin) do
  111.         begin
  112.           if fil_lin[i] <> ' ' then
  113.             begin
  114.               for i2:=i to length(fil_lin) do
  115.                 begin
  116.                   if fil_lin[i2] <> ' ' then
  117.                     first_word:=concat(first_word,upcase(fil_lin[i2]))
  118.                   else
  119.                     begin
  120.                       goto return;
  121.                     end; { else }
  122.                 end; { for }
  123.             end; { if }
  124.         end; { for }
  125.       return:
  126.     end; { get_first_word }
  127.  
  128.     procedure set_cont_flag(fil_lin:fil_lin_type;
  129.                             first_word:identifier_type;
  130.                             var is_cont_lin:boolean);
  131.     begin { set_cont_flag }
  132.       if (first_word = 'PROCEDURE') or
  133.          (first_word = 'FUNCTION') or
  134.          (first_word = 'PROGRAM') then
  135.         if (pos('(',fil_lin) <> 0) and (pos(')',fil_lin) = 0) then
  136.           is_cont_lin:=true;
  137.     end; { set_cont_flag }
  138.  
  139. begin { is_procfunc }
  140.   get_first_word(fil_lin,first_word);
  141.   if not is_cont_lin then set_cont_flag(fil_lin, first_word,is_cont_lin);
  142.   if (first_word = 'PROCEDURE') or
  143.       (first_word = 'FUNCTION') or
  144.       (first_word = 'PROGRAM') or
  145.       (first_word = 'END.') or
  146.       is_cont_lin then
  147.         is_procfunc:=true
  148.       else is_procfunc:=false;
  149. end; { is_procfunc }
  150.  
  151. procedure clrsav_cont_flag(fil_lin:fil_lin_type;
  152.                            var is_cont_lin:boolean);
  153. begin { clrsav_cont_flag }
  154.   if (pos(')',fil_lin) <> 0) then
  155.     is_cont_lin := false;
  156. end; { clrsav_cont_flag }
  157.  
  158. begin { print_procfunc_list }
  159.   writeln('                              *** LISTSUBS ***');
  160.   writeln;
  161.   writeln('                  A list of subprograms for the file ',filname);
  162.   writeln;
  163.   writeln;
  164.   writeln(outfil,'                              *** LISTSUBS ***');
  165.   writeln(outfil);
  166.   writeln(outfil,'                  A list of subprograms for the file ',filname);
  167.   writeln(outfil);
  168.   writeln(outfil);
  169.   is_cont_lin:=false;
  170.   while not eof(fil) do
  171.     begin
  172.       fil_lin:='';
  173.       readln(fil,fil_lin);
  174.       if is_procfunc(fil_lin,is_cont_lin) then
  175.         begin
  176.           writeln(fil_lin);
  177.           writeln(outfil,fil_lin);
  178.         end; { if }
  179.       if is_cont_lin then clrsav_cont_flag(fil_lin,is_cont_lin);
  180.   end; { while }
  181. end; { print_procfunc_list }
  182.  
  183. begin { main program }
  184.   done:=false;
  185.   repeat
  186.     error:=false;
  187.     get_filnames(filname,outfilname,done,error);
  188.     if (not done) and (not error) then
  189.       begin
  190.         open_file(filname,fil,error);
  191.         if not error then
  192.           begin
  193.             open_outfile(outfilname,outfil);
  194.             print_procfunc_list(fil,filname);
  195.             close_files(fil,outfil);
  196.           end;
  197.       end { if (not done) and (not error) then }
  198.   until done;
  199. end. { listSubs }